//
//  BTEListChoiseTableViewCell.swift
//  BTE
//
//  Created by Mike Zhang on 2025/9/2.
//

import UIKit

class BTEListChoiseTableViewCell: UITableViewCell {
    
    var model: [BTECarnepalaepanderismaciologyNetModel]?

    let box1 = UIView()
    let b1 = UILabel()
    let t1text = UITextField()
    let imagev = UIImageView.init(image: UIImage.init(named: "Vector"))
    
    var callBack:(()->())?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        
        box1.backgroundColor = .white
        box1.roundCorners(.allCorners, radius: 12)
        box1.layer.borderWidth = 3
        box1.layer.borderColor = UIColor.colorWithHex("#082252")!.cgColor
        
        contentView.addSubview(box1)
        box1.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(15)
            make.top.bottom.equalToSuperview().inset(12)
        }
        
        box1.addSubview(b1)
        b1.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(14)
            make.left.equalToSuperview().offset(16)
        }
        
        t1text.isUserInteractionEnabled = false
        t1text.font = .systemFont(ofSize: 14,weight: .bold)
        t1text.textColor = .black
        t1text.isUserInteractionEnabled = false
        box1.addSubview(t1text)
        t1text.snp.makeConstraints { make in
            make.left.equalTo(b1)
            make.right.equalToSuperview().offset(-32)
            make.height.equalTo(44)
            make.bottom.equalToSuperview()
            make.top.equalTo(b1.snp.bottom).offset(10)
        }
        
        box1.addSubview(imagev)
        imagev.snp.makeConstraints { make in
            make.width.equalTo(7)
            make.height.equalTo(14)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
        }
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(displayad))
        addGestureRecognizer(tap)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func displayad(){
        getTopViewController()?.view.endEditing(true)
        if let topViewController = getTopViewController() {
            let vc = BTESelectAlertViewController.init()
            vc.m = model
            vc.modalPresentationStyle = .overFullScreen
            topViewController.present(vc, animated: false, completion: nil)
            
            vc.action = {[weak self] in
                self?.callBack?()
                vc.dismiss(animated: false)
            }
            
        }
    }
    
    private func getTopViewController() -> UIViewController? {
            guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
                  let rootViewController = windowScene.windows.first?.rootViewController else {
                return nil
            }
            
            var topViewController = rootViewController
            while let presentedViewController = topViewController.presentedViewController {
                topViewController = presentedViewController
            }
            
            return topViewController
        }
    

}
